package com.simple.http.callback;

import android.util.Log;

import com.google.gson.Gson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


public abstract class RequestCallback<T> {
    private String TAG = getClass().getSimpleName();

    public void onResult(String data) {
        Log.e("http", data == null ? "" : data);
        Type type = null;
        try {
            Type[] dataTypes = ((ParameterizedType) getClass()
                    .getGenericSuperclass()).getActualTypeArguments();
            type = dataTypes != null && dataTypes.length > 0 ? dataTypes[0] : null;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (type == null) {
            return;
        }
        try {
            response(new Gson().fromJson(data, type));
        } catch (Exception e) {
            Log.e("http", e.toString());
        }
    }

    public abstract void response(T data);
}
